import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;

// Tells the applet you will be using the ActionListener methods.

public class GUI extends Applet
{
         // gui buttons
     Button searchButton;
     Button clearButton;
     // search field
     TextField searchText;
     // search field
     TextField pageText;
     // top numbers of words field
     TextField topText;
     // search field
     TextField perPageText;
     // radio button group
     CheckboxGroup radioGroup;
     Checkbox generalRadio;
     Checkbox hashRadio;
     Checkbox pieRadio;
     Checkbox barRadio;
     Checkbox writeRadio;
     // graphics
     Graphics bufferGraphics;
     // bufferGraphics.
     Image offscreen;
     // To get the width and height of the applet.
     Dimension dim;
     int curX, curY;
     // observers of buttons
     ButtonListener searchListener;
     ButtonListener clearListener;
     
     public void init()
     {
         // new Flow Layout
          setLayout(new FlowLayout());
          setSize(450,200);
         
          // buttons for text management
          searchButton = new Button("Search");
          clearButton = new Button("Clear Text");
         
          // display search
          searchText = new TextField("",35);
          // display number of pages to get
          pageText = new TextField("10",3);
          // display results per page to get
          perPageText = new TextField("10",3);
          // display results per page to get
          topText = new TextField("5",3);
         
          // radio button group
          radioGroup = new CheckboxGroup();
          generalRadio = new Checkbox("General", radioGroup, true);
          hashRadio = new Checkbox("Hashtag", radioGroup, false);
          pieRadio = new Checkbox("Pie Chart", true);
          barRadio = new Checkbox("Bar Graph", false);
          writeRadio = new Checkbox("Write to File", false);
          
          // add everything to gui
          add(searchButton);
          add(clearButton);
          add(pieRadio);
          add(barRadio);
          
          add(generalRadio);
          add(hashRadio);
          add(pageText);
          add(perPageText);
          add(pieRadio);
          add(barRadio);
          add(writeRadio);
          add(searchText);
          add(topText);
          
          // attatch listeners to buttons
          searchListener = new ButtonListener(this, searchButton, "search");
          clearListener = new ButtonListener(this, clearButton, "clear");
         
          dim = getSize();
         
          // background color
          setBackground(Color.white);
         
          // Create an offscreen image to draw on
          offscreen = createImage(dim.width,dim.height);
         
          // written on the offscreen image.
          bufferGraphics = offscreen.getGraphics();
         
     }
     
     // paints the gui
     public void paint(Graphics g)
     {
        bufferGraphics.clearRect(0,0,dim.width,dim.width);
       
        g.drawImage(offscreen,0,0,this);
     }
     // called every time to update screen
     public void update(Graphics g)
     {
          paint(g);
     }
     
     // called by a button observer
     public void buttonClicked(String type) {
         if (type == "search") {
                 searchTerm(searchText);
         }
         else if (type == "clear") {
                 clearTerm();
         }
         
     }
     
     // clears input text
     public void clearTerm() {
         searchText.setText("");
     }
     
     private int toInt(String error, TextField item) {
         int value;
         
         try {
                 // if negative input, make positive
                 value = Math.abs(Integer.parseInt(item.getText()));
         }
         catch (NumberFormatException e) {
                 System.out.println("Incorrectly formatted " + error + " number, defualt is 10");
                 value = 10;
         }
         
         return value;
     }
     
     // searches twitter via Searcher with specified parameters
     public void searchTerm(TextField term) {
         // create new twitter searcher
         int page = toInt("page", pageText);
         int perPage = toInt("results per page", perPageText);
         int top = toInt("top results", topText);
         
         Searcher search = new Searcher(term.getText(), page, perPage);
         
         // enables a hashtag search
         if (hashRadio.getState())
        	 search.enableHashSearch();
         
         // search for tweets
         search.searchTweets();
         DataProcessor dp = new DataProcessor(search.getResults());
         
         String[] a = dp.nTopWords(top);
         
         // pop up window for pie chart
         if (pieRadio.getState()) {
             GraphWindow pwindow = new PieWindow(top);
        	 makeGraph(pwindow, a, top, dp);
         }
         
         // display bar graph
         if (barRadio.getState()) {
        	 GraphWindow bwindow = new BarWindow(top);
        	 makeGraph(bwindow, a, top, dp);
         }
         
         // write file
         if (writeRadio.getState()) {
        	 search.writeSearchToFile("TwitterData.txt");
         }
     }
     
     public void  makeGraph(GraphWindow w, String[] a, int top, DataProcessor dp) {
    	 w.calculate(a, top, dp);
         // pop up graph window
         w.display();
     
     }

}
